package com.elitel.frame.base.component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.elitel.frame.base.service.BasicServerService;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
//import com.elitel.common.redis.RedisDbIndex;
//import com.elitel.common.redis.service.RedisCacheService;
import com.elitel.frame.main.entity.BaseServices;
import com.elitel.frame.main.entity.CodeSystem;
import com.elitel.frame.main.entity.vo.PageResponseVo;

@Component("redisCatchBasicServer")
public class RedisCatchBasicServer {
    @Resource
    private BasicServerService basicServerService;

    //	@Resource
//	private RedisCacheService redisCacheService;
    @Resource
    private CacheCollections cacheCollections;

    //	public PageResponseVo<BaseServices> getRedisCatchBasicServer(String key,
//			String pageNo, String pageSize) {
//		int rdi = RedisDbIndex.value;
//		String PageResponse = redisCacheService.getObject(key, rdi);
//		PageResponseVo<BaseServices> pageResponseVo = new PageResponseVo<BaseServices>();
//		Map<String,Object> map = JSON.parseObject(PageResponse,Map.class);
//		int count = (int) map.get("total");
//		List<BaseServices> list = JSON.parseArray(JSON.parseObject(PageResponse).getString("rows"), BaseServices.class);
//		int a = Integer.parseInt(pageNo);
//		int b = Integer.parseInt(pageSize);
//		int lineStart = (a-1)*b;
//		int lineEnd = a*b;
//		List<BaseServices> basicServerList = new ArrayList<BaseServices>();
//		for (int i = lineStart; i <(lineEnd-lineStart<list.size()-lineEnd?lineEnd:list.size()); i++) {
//			basicServerList.add(list.get(i));
//		}
//		pageResponseVo.setRows(basicServerList);
//		pageResponseVo.setTotal(count);
//		return pageResponseVo;
//	}
    public PageResponseVo<BaseServices> getRedisCatchBasicServer(String pageNo, String pageSize) {
        String basicServerStr = cacheCollections.getBasicServerStr();
        PageResponseVo<BaseServices> pageResponseVo = new PageResponseVo<BaseServices>();
        Map<String, Object> map = JSON.parseObject(basicServerStr, Map.class);
        int count = (int) map.get("total");
        List<BaseServices> list = JSON.parseArray(JSON.parseObject(basicServerStr).getString("rows"), BaseServices.class);
        int a = Integer.parseInt(pageNo);
        int b = Integer.parseInt(pageSize);
        int lineStart = (a - 1) * b;
        int lineEnd = a * b;
        List<BaseServices> basicServerList = new ArrayList<BaseServices>();
        for (int i = lineStart; i < (lineEnd - lineStart < list.size() - lineEnd ? lineEnd : list.size()); i++) {
            basicServerList.add(list.get(i));
        }
        pageResponseVo.setRows(basicServerList);
        pageResponseVo.setTotal(count);
        return pageResponseVo;
    }

    @CachePut(value = CacheConfig.DEFAULT, key = "'BASICSERVER'")
    public String refreshBasicServer() {
        PageResponseVo<BaseServices> pageResponseVos = basicServerService.selectBasicServerAll();
        String basicServerString = JSON.toJSONString(pageResponseVos);
        return basicServerString;
    }
}
